
--[ngx weixin auth case]
 
/***
*   ngx 模块需要在nginx上下文中执行
*   web 模式只预览代码并不执行。
*/

var http = require("resty.http");

ngx.header["Content-Type"] = "text/plain";


var data = ngx.req.get_uri_args();

var code = data["code"];      
    
var appId= "aaa";

var appSe= "bbb";

var httpc = http.new();
var url   ="https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+appSe+"&js_code="+code+"&grant_type=authorization_code"
var res, err= httpc >>> request_uri(url, {
    "ssl_verify":false,
    "method":"GET"
})

if(res == nil) {
    ngx.log(ngx.ERR, "err:auth failed.");
    ngx.exit(502);
}

if(res.status == 200) {
    ngx.say(res.body);
} else {
    ngx.exit(502);
}
